layui.use(['transfer', 'layer', 'util'], function(){
    var $ = layui.$
        ,transfer = layui.transfer
        ,layer = layui.layer
        ,util = layui.util;

    //窜梭框生成数据对象
    var factoryrForObj = function (){
        var item = new Object();
        item = {"value": "", "title": "", "disabled": "", "checked": ""};
        return item;
    }

    //获取权限信息
    var loadRolePermission = function () {
        var rolePermission;

        $.ajax({
            url:'/role-permission/getAllRolePermission',
            type:'POST', //GET
            async:false,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            contentType: "application/json",
            success:function(data){
                if (data.code =='0' && data.data!=null){
                    rolePermission = data.data;
                }
            }
        });
        return rolePermission;
    };

    //获取角色信息
    var loadRoleInfo = function () {
        var roleInfo;

        $.ajax({
            url:'/role/getAllRoleInfo',
            type:'POST', //GET
            async:false,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            contentType: "application/json",
            success:function(data){
                if (data.code =='0' && data.data!=null){
                    roleInfo = data.data;
                }
            }
        });
        return roleInfo;
    };


    //数据处理
    /*
    * 核心，将角色表，权限表，角色权限表数据查出来做动态展示
    *
    *
    *
    * */
    var processData =function (data) {

        var rolePermission = {};

        var arr = new Array();
        $.each(data,function (index,value){
            var item = factoryrForObj();
            item.value = value.permissionId;
            item.title = value.name;
            arr.push(item);
        });

        //获取权限信息
        var rolePermission = loadRolePermission();
        //获取角色信息
        var allRoleInfo = loadRoleInfo();


        $.each(allRoleInfo,function (index,value) {
            //穿梭框右侧值
            var values = [];
            var html = '';
            html += '<div  style="width:40%; height:auto; float:left; display:inline">' +
                '<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">\n' +
                '  <legend>《'+value.description+'》菜单维护'+'</legend>\n' +
                '</fieldset>' +
                '<div id="'+value.roleId +'"></div>'+
                '</div>';

            $('#role-per').append(html);
            //动态标题
            var tit = [];
            tit[0] = value.description+'不能访问';
            tit[1] = value.description+'可以访问';
            //为穿梭框左侧赋初始值，来源于数据库
            $.each(rolePermission,function (index,val) {
                if (val.roleId == value.roleId){
                    values.push(val.permissionId);
                }

            });
            //渲染
            transfer.render({
                elem: '#'+ value.roleId  //绑定元素
                ,title: tit
                ,data: arr
                ,id: value.roleId //定义索引
                ,value: values
                ,onchange: function (obj,index){
                    if (index == 0){

                        var permissions = new Array();
                        $.each(obj,function (index,val){
                            permissions.push(val.value);
                        });
                        /* $.post("/role-permission/addRolePermission/"+value.roleId,JSON.stringify(permissions),function (data) {
                            if (data.code == '0'){
                                layer.msg(tit+"权限新增成功！",{icon:6});
                            }
                         });*/

                        $.ajax({
                            url:"/role-permission/addRolePermission/"+value.roleId,
                            type:'POST',
                            async:true,
                            data: JSON.stringify(permissions),
                            timeout:5000,    //超时时间
                            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                            contentType: "application/json",
                            success:function(data){
                                if (data.code == '0'){
                                    layer.msg(value.description+"权限新增成功！",{icon:6});
                                }
                            }
                        });

                    }else if (index = 1){  //权限移除操作
                        var permissions = new Array();
                        $.each(obj,function (index,val){
                            permissions.push(val.value);
                        });
                        /* $.post("/role-permission/addRolePermission/"+value.roleId,JSON.stringify(permissions),function (data) {
                            if (data.code == '0'){
                                layer.msg(tit+"权限新增成功！",{icon:6});
                            }
                         });*/

                        $.ajax({
                            url:"/role-permission/removeRolePermission/"+value.roleId,
                            type:'POST',
                            async:true,
                            data: JSON.stringify(permissions),
                            timeout:5000,    //超时时间
                            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
                            contentType: "application/json",
                            success:function(data){
                                if (data.code == '0'){
                                    layer.msg(value.description+"权限删除成功！",{icon:6});
                                }
                            }
                        });
                    }
                }
            });
        });

    };


    var loadAllMenu = function () {
        $.ajax({
            url:'/permission/getAllPermission',
            type:'POST', //GET
            async:true,    //或false,是否异步
            timeout:5000,    //超时时间
            dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
            contentType: "application/json",
            success:function(data){
                if (data.code =='0' && data.data!=null){
                    processData(data.data);
                }else {
                    layer.msg("系统错误！请重新加载！",{icon:2});
                }
            }
        });
    };

    loadAllMenu();


});